<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 登录退出
 */
class AdminController extends Controller {
	/**
	 * 检查是否登录
	 * @return [type] [description]
	 */
	public function checkLogin()
	{
		if (is_null(session('admin.username')) && is_null(cookie('id'))) {
			alertMes('请先登录',  U('admin/login'));
		} else {
			if (session('admin.username')) {
				$info = session('admin');
			} else {
				$infoid = cookie('id');
				$Admin = M('Admin');
				$res = $Admin->where('id=' . $infoid)->field('id,username,is_admin,headimg')->find();
				session('admin.username', $res['username']);
				session('admin.id', $res['id']);
				session('admin.headimg', $res['headimg']);
				session('admin.is_admin', $res['is_admin']);
				$info = session('admin');
			}
			return $info;
		}
	}
	/**
	 * 首页显示
	 * @param  string  $order [description]
	 * @param  integer $p     [description]
	 * @return [type]         [description]
	 */
	public function index($order = 'id',$p = 1){
		//首页用户列表
		$Admin = M('Admin');
		$res = $Admin
			->field('id,username,email,lastdate')
			->order($order)
			->page($p,5)
			->select();
		$this->assign('res', $res);
		$count = $Admin->count();
		$page = new \Think\Page($count,5);
		$page->setConfig('prev','<i class="fa fa-chevron-left"></i>');
		$page->setConfig('next','<i class="fa fa-chevron-right"></i>');
		$show = $page->show();
		$this->assign('page', $show);

		//首页文章
		$Article = M('Article');
		//最新文章
		$newArt = $Article->field('id,title,profiles,smallimg')->order('creattime desc, updatetime desc')->find();
		$this->assign('newArt', $newArt);
		//点击量最高文章
		$hitsArt = $Article->field('id,title,profiles,smallimg')->order('hits desc, updatetime desc')->find();
		$this->assign('hitsArt', $hitsArt);
		$this->assign('admininfo', $this->checkLogin());
		$this->display();
	}
	/**
	 * 管理员登录
	 * @return [type] [description]
	 */
	public function login()
	{
		if (IS_POST) {
			$Admin = D('Admin');
			if (!$Admin->create(I('post.'),4)) {
				alertMes($Admin->getError(), U('admin/login'));
			} else {
				$arr['username'] = I('post.username');
				$arr['password'] = md5(I('post.password'));
				$res = $Admin->where($arr)->field('id,username,password,is_admin,headimg')->find();
				if ($res) {
					session('admin.username', $res['username']);
					session('admin.id', $res['id']);
					session('admin.headimg', $res['headimg']);
					session('admin.is_admin', $res['is_admin']);
					if (I('post.autoFlag')) {
						cookie('id', $res['id'], 'expire=604800&path=' . __ROOT__ . '/');
					}
					$time['lastdate'] = time();
					$Admin->where('id='.$res['id'])->setInc('loginnum');
					$Admin->where('id='.$res['id'])->save($time);
					$this->redirect('/admin');
				} else {
					alertMes('用户名或密码错误',  U('admin/login'));
				}
			}
		} else {
			$this->display();
		}
	}
	/**
	 * 退出登录
	 * @return [type] [description]
	 */
	public function logout()
	{
		session(null);
		setcookie('id','',time()-3600, __ROOT__ . '/');
		$this->redirect('/admin/login');
	}
	/**
	 * 获取验证码
	 * @return string [description]
	 */
	public function getVerification()
	{
		return getVerification();
	}
}